﻿@using System.Reactive.Disposables
@using Blazor.Core;
@using Blazor.Localization
@using Blazor.ViewModels.UpdateSetting;
@inherits SoraniComponentBase<SettingsViewModel>
@inject IStringLocalizer<UpdateSettingLocalization> Loc

@if (ViewModel is not null)
{
    @if (ViewModel.CurrentSetting is not null)
    {
        <Flex Direction="FlexDirection.Horizontal" Justify="FlexJustify.SpaceBetween">
            <Menu Mode="MenuMode.Horizontal"
                  OnMenuItemClicked="NativeVersionClickHandler"
                  DefaultSelectedKeys="new []{ViewModel.CurrentSetting.NativeVersion.ToString()}">
                @foreach (var c in ViewModel.NativeVersions.OrderByDescending(i => i))
                {
                    <MenuItem Key="@c.ToString()">@($"{Loc[UpdateSettingLocalization.NativeVersion]} {c}")</MenuItem>
                }
            </Menu>
            <Space>
                <SpaceItem>
                    <Button Loading="ViewModel.IsNewingNativeVersion"
                            OnClick="() => ViewModel.NewNativeVersion.Execute().Subscribe().DisposeWith(Disposables)">
                        @CommonLoc[CommonLocalization.Add]
                    </Button>
                </SpaceItem>
                <SpaceItem>
                    <Button Danger Loading="ViewModel.IsDeletingNativeVersion"
                            OnClick="() => ViewModel.Delete.Execute().Subscribe().DisposeWith(Disposables)">@CommonLoc[CommonLocalization.Delete]</Button>
                </SpaceItem>
            </Space>
        </Flex>

        <SingleSetting ViewModel="ViewModel.CurrentSetting"/>
    }
    else
    {
        <empty>
            <Button Type="ButtonType.Primary"
                    Loading="ViewModel.IsNewingNativeVersion"
                    OnClick="() => ViewModel.NewNativeVersion.Execute().Subscribe().DisposeWith(Disposables)">
                Create Now
            </Button>
        </empty>
    }
}
else
{
    <Empty/>
}
